﻿Imports System.Xml
Imports System.IO

Module modXML
    Public Enum xmlCharConvert

        xmlConvertToWrite = 1
        xmlConvertToRead = 0
    End Enum

    Public Sub XML_CreateNode(ByRef xtw As XmlTextWriter, ByVal xmlTag As String, ByVal xmlValue As String)

        xtw.WriteStartElement(xmlTag)
        xtw.WriteString(xmlValue)
        xtw.WriteEndElement()
    End Sub

    Public Function XML_ConvertText(ByVal xmlValue As String,
                                    Optional ByRef convertType As xmlCharConvert = xmlCharConvert.xmlConvertToWrite) _
        As String

        Dim _value As String = xmlValue

        If _value = Nothing Then Return _value

        If convertType = xmlCharConvert.xmlConvertToWrite Then

            _value.Replace("&", "&amp;")
            _value.Replace("<", "&lt;")
            _value.Replace(">", "&gt;")
            _value.Replace("""", "&quot;")
            _value.Replace("'", "&apos;")

        Else
            _value.Replace("&amp;", "&")
            _value.Replace("&lt;", "<")
            _value.Replace("&gt;", ">")
            _value.Replace("&quot;", """")
            _value.Replace("&apos;", "'")

        End If

        Return _value
    End Function

    Public Sub Trinity_ChangeSetting(ByVal pathVarDemonbuddy As String, ByVal tagSettingName As String,
                                     ByVal tagProperty As String, ByVal value As String)

        Dim pathVar As String = Path.Combine(pathVarDemonbuddy, "\Settings\TVars.xml")

        If File.Exists(pathVar) = False Then
            pathVar = File_FixPath(pathVar, True, My.Settings.FolderDemonbuddy)
            If File.Exists(pathVar = False) Then
                MsgBox("Failed attempting to change Trinity settings. Could not find XML file.",
                       vbOKOnly + vbExclamation)
                Exit Sub
            End If
        End If

        Dim xDoc As XmlDocument = New XmlDocument()
        xDoc.Load(pathVar)
        Dim xTVars As XmlNode = xDoc.ChildNodes(1)
        Dim xRoot As XmlNode = xTVars.FirstChild

        Dim i As Integer
        Dim tmpSettingName As String
        Dim iProp As Integer
        Dim tmpPropertyName As String

        For i = 0 To (xRoot.ChildNodes.Count - 1)
            tmpSettingName = xRoot.ChildNodes(i).FirstChild.InnerText
            If tmpSettingName = tagSettingName Then
                For iProp = 0 To (xRoot.ChildNodes(i).ChildNodes(1).ChildNodes.Count - 1)
                    tmpPropertyName = xRoot.ChildNodes(i).ChildNodes(1).ChildNodes(iProp).Name
                    If tmpPropertyName = tagProperty Then
                        xRoot.ChildNodes(i).ChildNodes(1).ChildNodes(iProp).InnerText = value
                        xDoc.Save(pathVar)
                        Exit Sub
                    End If
                Next
            End If
        Next
    End Sub

    Public Sub XML_ReadMenuData(ByVal pathFile As String, ByRef arrOutput As xmlInformation(),
                                Optional ByVal arrIndex As Integer = 0,
                                Optional ByVal boolAllowSubstitutions As Boolean = True)

        Dim _xmlDoc As XmlDocument = New XmlDocument
        Dim _xmlNodeList As XmlNodeList
        Dim _xmlNode As XmlNode

        Dim _id As Integer
        Dim _tag As String
        Dim _value As String
        Dim _count As Integer

        Try

            _xmlDoc.Load(pathFile)
            _xmlNodeList = _xmlDoc.SelectNodes("/menu_info/menu")

            For Each _xmlNode In _xmlNodeList

                For i As Integer = 0 To (_xmlNode.ChildNodes.Count - 1)

                    _id = arrIndex
                    _tag = _xmlNode.ChildNodes.Item(i).LocalName
                    _value = _xmlNode.ChildNodes.Item(i).InnerXml

                    If (String_IsEmpty(_tag) = False) And (String_IsEmpty(_value) = False) Then

                        _count = Array_Count(arrOutput)

                        ReDim Preserve arrOutput(_count)

                        arrOutput(_count).idx = _id
                        arrOutput(_count).tag = _tag
                        arrOutput(_count).value = _value

                    End If

                Next i

            Next _xmlNode

        Catch errorVariable As Exception

            Throw New Exception("Failed! Error: " & errorVariable.ToString())

        End Try
    End Sub

    Public Sub XML_ReadPatchData(ByVal pathFile As String, ByRef arrOutput As xmlInformation(),
                                 Optional ByVal arrIndex As Integer = 0,
                                 Optional ByVal boolAllowSubstitutions As Boolean = True,
                                 Optional ByVal boolAddPath As Boolean = True)

        Dim _xmlDoc As XmlDocument = New XmlDocument()
        Dim _xmlNodeList As XmlNodeList
        Dim _xmlNode As XmlNode

        Dim _id As Integer
        Dim _tag As String
        Dim _value As String

        Dim _count As Integer

        Try

            _xmlDoc.Load(pathFile)
            _xmlNodeList = _xmlDoc.SelectNodes("/patch_info/patch")

            For Each _xmlNode In _xmlNodeList

                For i As Integer = 0 To (_xmlNode.ChildNodes.Count - 1)

                    _id = arrIndex
                    _tag = _xmlNode.ChildNodes.Item(i).LocalName
                    _value = _xmlNode.ChildNodes.Item(i).InnerXml

                    If (String_IsEmpty(_tag) = False) And (String_IsEmpty(_value) = False) Then

                        _count = Array_Count(arrOutput)

                        ReDim Preserve arrOutput(_count)

                        arrOutput(_count).idx = _id
                        arrOutput(_count).tag = _tag
                        arrOutput(_count).value = _value

                    End If

                Next i

            Next _xmlNode

        Catch errorVariable As Exception

            Throw New Exception("Failed! Error: " & errorVariable.ToString())

        End Try

        If boolAddPath = True Then
            Array_SetValueByTag(arrOutput, arrIndex, "path", Directory_GetParent(pathFile))
        End If
    End Sub
End Module